package org.jdsm.flow.gxt.users.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.core.client.util.ToggleGroup;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.button.ToggleButton;
import com.sencha.gxt.widget.core.client.container.CardLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VBoxLayoutContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import org.jdsm.flow.gxt.users.client.widgets.RoleLayoutWidget;
import org.jdsm.flow.gxt.users.client.widgets.UserLayoutWidget;

import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: mikom
 * Date: 9/28/12
 * Time: 11:58 AM
 */
public class AllocatePermissions implements IsWidget {


    @UiField(provided = true)
    protected UserLayoutWidget userLayoutWidget;

    @UiField(provided = true)
    protected RoleLayoutWidget roleLayoutWidget;
    @UiField
    ToggleButton usersBtn, rolesBtn;
    @UiField
    VBoxLayoutContainer buttonBox;
    @UiField
    CardLayoutContainer layout;

    @Override
    public Widget asWidget() {
        Widget rootElement = ourUiBinder.createAndBindUi(this);
        ToggleGroup toggleGroup = new ToggleGroup();
        toggleGroup.add(usersBtn);
        toggleGroup.add(rolesBtn);
        return rootElement;
    }

    @UiHandler({"usersBtn", "rolesBtn"})
    public void buttonClicked(SelectEvent event) {
        ToggleButton button = (ToggleButton) event.getSource();

        int index = buttonBox.getWidgetIndex(button);
        layout.setActiveWidget(layout.getWidget(index + 1));
    }

    interface AllocatePermissionsUiBinder extends UiBinder<Widget, AllocatePermissions> {
    }

    private static AllocatePermissionsUiBinder ourUiBinder = GWT.create(AllocatePermissionsUiBinder.class);

    public AllocatePermissions() {
        Map<String, String> config = RemoteConfiguration.getConfig();

        userLayoutWidget = new UserLayoutWidget(
                config.get("getUsers"),
                config.get("getRoles"),
                config.get("getUserRoles"),
                config.get("saveRoleForUser"),
                config.get("getGroups"),
                config.get("getUserGroups"),
                config.get("saveUserGroups"));
        roleLayoutWidget = new RoleLayoutWidget(
                config.get("getRoles"),
                config.get("getUsers"),
                config.get("getRoleUsers"),
                config.get("saveRole"),
                config.get("saveUserForRole"));





    }


 /*   @UiFactory
    public ContentPanel createContentPanel(ContentPanel.ContentPanelAppearance appearance) {
        return new ContentPanel(appearance);
    }*/
}